package be.jprojects.tests.filter;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import be.jprojects.subscription.gui.StatusBar;
import be.jprojects.subscription.gui.StatusBar.Status;
import be.jprojects.subscription.logic.BusinessLogicController;
import be.jprojects.subscription.logic.interfaces.ICity;
import be.jprojects.subscription.logic.interfaces.ICountry;
import be.jprojects.subscription.logic.interfaces.IProvince;
import be.jprojects.subscription.logic.interfaces.IStreet;

public class FilterTester extends JFrame implements ListSelectionListener {
	private FilteredJList countryList, provinceList, cityList, streetList;
	private List<ICountry> countries;
	private StatusBar statusBar;

	public FilterTester() {
		super("Filter Tester");
		countries = null;
		try {
			countries = BusinessLogicController.getInstance().getCountries();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		getContentPane().setLayout(new BorderLayout());
		JPanel centerPanel = new JPanel(new GridLayout(1, 4));

		countryList = new FilteredJList();
		countryList.addListSelectionListener(this);
		JScrollPane countryPane = new JScrollPane(countryList,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		JPanel countryPanel = new JPanel(new BorderLayout());
		countryPanel.add(countryPane, BorderLayout.CENTER);
		countryPanel.add(countryList.getFilterField(), BorderLayout.NORTH);
		countryPanel.add(countryList.getFilterLabel(), BorderLayout.SOUTH);

		provinceList = new FilteredJList();
		provinceList.addListSelectionListener(this);
		JScrollPane provincePane = new JScrollPane(provinceList,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		JPanel provincePanel = new JPanel(new BorderLayout());
		provincePanel.add(provincePane, BorderLayout.CENTER);
		provincePanel.add(provinceList.getFilterField(), BorderLayout.NORTH);
		provincePanel.add(provinceList.getFilterLabel(), BorderLayout.SOUTH);

		cityList = new FilteredJList();
		cityList.addListSelectionListener(this);
		JScrollPane cityPane = new JScrollPane(cityList,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		JPanel cityPanel = new JPanel(new BorderLayout());
		cityPanel.add(cityPane, BorderLayout.CENTER);
		cityPanel.add(cityList.getFilterField(), BorderLayout.NORTH);
		cityPanel.add(cityList.getFilterLabel(), BorderLayout.SOUTH);
		
		streetList = new FilteredJList();
		streetList.addListSelectionListener(this);
		JScrollPane streetPane = new JScrollPane(streetList,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		JPanel streetPanel = new JPanel(new BorderLayout());
		streetPanel.add(streetPane, BorderLayout.CENTER);
		streetPanel.add(streetList.getFilterField(), BorderLayout.NORTH);
		streetPanel.add(streetList.getFilterLabel(), BorderLayout.SOUTH);

		centerPanel.add(countryPanel);
		centerPanel.add(provincePanel);
		centerPanel.add(cityPanel);
		centerPanel.add(streetPanel);

		getContentPane().add(centerPanel, BorderLayout.CENTER);
		
		statusBar = new StatusBar();
		getContentPane().add(statusBar, BorderLayout.SOUTH);
		
		pack();
		setVisible(true);

		loadCountries();
		loadProvinces(null);
		loadCities(null);
		loadStreets(null);
	}

	private void loadStreets(ICity city) {
		streetList.clear();
		
		if (city != null) {
			if (!city.isFullyLoaded()) {
				try {
					BusinessLogicController.getInstance().loadStreets(city);
					statusBar.setText("", Status.nothing);
				} catch (Exception e) {
					statusBar.setText(e.getMessage(), Status.error);				
					e.printStackTrace();
				}
			}
			for (IStreet street: city.getStreets()) {
				streetList.addItem(street);
			}
		}
	}
	
	private void loadCities(IProvince province) {
		cityList.clear();
		System.out.println("stratenlijst clearen");
		streetList.clear();
		
		if (province != null) {
			if (!province.isFullyLoaded()) {
				try {
					BusinessLogicController.getInstance().loadCities(province);
					statusBar.setText("", Status.nothing);
				} catch (Exception e) {
					statusBar.setText(e.getMessage(), Status.error);				
					e.printStackTrace();
				}
			}
			for (ICity city: province.getCities()) {
				cityList.addItem(city);
			}
		}
	}

	private void loadProvinces(ICountry country) {
		provinceList.clear();
		cityList.clear();
		streetList.clear();
		
		if (country != null) {
			if (!country.isFullyLoaded()) {
				try {
					BusinessLogicController.getInstance().loadProvinces(country);
					statusBar.setText("", Status.nothing);
				} catch (Exception e) {
					statusBar.setText("Fout bij het laden van de provincies", Status.error);		
				}
			}
			for (IProvince province: country.getProvinces()) {
				provinceList.addItem(province);
			}
		}
	}

	private void loadCountries() {
		countryList.clear();
		provinceList.clear();
		cityList.clear();
		streetList.clear();
		
		for (ICountry country : countries) {
			countryList.addItem(country);
		}
	}

	public static void main(String[] args) {
		new FilterTester();
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		
		JList list = (JList) e.getSource();
		if (list.equals(countryList)) {
			loadProvinces((ICountry) list.getSelectedValue());
		} else if (list.equals(provinceList)) {
			loadCities((IProvince) list.getSelectedValue());
		} else if (list.equals(cityList)) {
			loadStreets((ICity) list.getSelectedValue());
		} else if (list.equals(streetList)) {
			System.out.println((IStreet) list.getSelectedValue());
		} else {
			countryList.clear();
			provinceList.clear();
			cityList.clear();
			streetList.clear();
		}
	}

}
